package com.todo.message.application.backend.controller;


import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.text.StrPool;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.message.iface.vo.amis.CommonAmisVo;
import com.todo.common.common.constants.AustinConstant;
import com.todo.message.infrastructure.util.LoginUtils;
import com.todo.message.infrastructure.po.ChannelAccount;
import com.todo.message.application.convert.Convert4Amis;
import com.todo.message.application.service.ChannelAccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 渠道账号管理接口
 *
 * @author 3y
 */
@Slf4j
@RestController
@RequestMapping("/bd/account")
@Tag(name = "渠道账号管理接口")
public class ChannelAccountController {

    @Autowired
    private ChannelAccountService channelAccountService;

    @Autowired
    private LoginUtils loginUtils;

    /**
     * 如果Id存在，则修改
     * 如果Id不存在，则保存
     */
    @PostMapping("/save")
    @Operation(summary = "/保存数据")
    public BizResponse<ChannelAccount> saveOrUpdate(@RequestBody ChannelAccount channelAccount) {
        if (loginUtils.needLogin() && CharSequenceUtil.isBlank(channelAccount.getCreator())) {
            throw new BizException(BizErrorCodeEnum.AUTH_FAILED);
        }
        channelAccount.setCreator(CharSequenceUtil.isBlank(channelAccount.getCreator()) ? AustinConstant.DEFAULT_CREATOR : channelAccount.getCreator());

        return BizResponse.success(channelAccountService.save(channelAccount));
    }

    /**
     * 根据渠道标识查询渠道账号相关的信息
     */
    @GetMapping("/queryByChannelType")
    @Operation(summary = "/根据渠道标识查询相关的记录")
    public BizResponse<List<CommonAmisVo>> query(Integer channelType, String creator) {
        if (loginUtils.needLogin() && CharSequenceUtil.isBlank(creator)) {
            throw new BizException(BizErrorCodeEnum.AUTH_FAILED);
        }
        creator = CharSequenceUtil.isBlank(creator) ? AustinConstant.DEFAULT_CREATOR : creator;

        List<ChannelAccount> channelAccounts = channelAccountService.queryByChannelType(channelType, creator);
        return BizResponse.success(Convert4Amis.getChannelAccountVo(channelAccounts, channelType));
    }

    /**
     * 所有的渠道账号信息
     */
    @GetMapping("/list")
    @Operation(summary = "/渠道账号列表信息")
    public BizResponse<List<ChannelAccount>> list(String creator) {
        if (loginUtils.needLogin() && CharSequenceUtil.isBlank(creator)) {
            throw new BizException(BizErrorCodeEnum.AUTH_FAILED);

        }
        creator = CharSequenceUtil.isBlank(creator) ? AustinConstant.DEFAULT_CREATOR : creator;

        return BizResponse.success(channelAccountService.list(creator));
    }

    /**
     * 根据Id删除
     * id多个用逗号分隔开
     */
    @DeleteMapping("delete/{id}")
    @Operation(summary = "/根据Ids删除")
    public void deleteByIds(@PathVariable("id") String id) {
        if (CharSequenceUtil.isNotBlank(id)) {
            List<Long> idList = Arrays.stream(id.split(StrPool.COMMA)).map(Long::valueOf).collect(Collectors.toList());
            channelAccountService.deleteByIds(idList);
        }
    }

}
